package Stack;

public class MyLinkedListStack {

    Node head = null;
    int size = 0;
    class Node{
        int data;
        Node next;

        public Node(int data, Node next) {
            this.data = data;
            this.next = next;
        }
    }

    //入栈操作,头插
    public void push(int data){

        //如果还没有节点的情况
        if (head == null){
            head = new Node(data,null);
            size++;
            System.out.println("入栈成功");
            return;
        }
        //如果存在头节点,进行头插处理
        head = new Node(data,head);
        size++;
        System.out.println("入栈成功");
    }

    //出栈操作
    public int pop(){

        //判断还没有节点的情况
        if(head == null){
            System.out.println("出栈操作失败");
            return -1;
        }
        //如果有节点的情况
        //头删处理
        Node temp = head.next;
        int ret = head.data;
        head = temp;
        size--;
        System.out.println("出栈操作成功");
        return ret;
    }

    //查看栈顶元素
    public int peek(){

        if(head == null){
            System.out.println("查看栈顶元素操作失败");

            return -1;

        }
        return head.data;
    }

    //打印栈的全部元素
    public void printAll(){
        Node temp = head;
        while (temp != null){
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
    }
}
